package org.example.fastproject.web.sys.service.impl;

import cn.hutool.json.JSONUtil;
import org.example.fastproject.core.constant.RedisConstant;
import org.example.fastproject.web.sys.model.vo.menu.MenuNode;
import org.example.fastproject.web.sys.model.vo.user.UserLoginVo;
import org.example.fastproject.web.sys.model.vo.user.UserVo;
import org.example.fastproject.web.sys.service.SysCacheService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @Author 王海舟
 * @Date 2023/1/16 13:05
 * @PackageName:org.example.fastproject.web.sys.service.impl
 * @ClassName: SysCacheServiceImpl
 * @Description: 缓存服务实现类
 * @Version 1.0
 */
@Service
public class SysCacheServiceImpl implements SysCacheService {
    @Resource
    private RedisTemplate<String,Object> redisTemplate;

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public void setLogin(Long userId,String token) {
        String key = RedisConstant.USER_TOKEN_PREFIX+userId;
        redisTemplate.opsForValue().set(key,token,1, TimeUnit.HOURS);
    }

    @Override
    public String getLogin(Long userId) {
        String key = RedisConstant.USER_TOKEN_PREFIX+userId;
        String token = (String) redisTemplate.opsForValue().get(key);
        redisTemplate.expire(key,1,TimeUnit.HOURS);
        return token;
    }

    @Override
    public void removeLogin(Long userId) {
        String key = RedisConstant.USER_TOKEN_PREFIX+userId;
        redisTemplate.delete(key);
    }

    @Override
    public void setMenuTree(List<MenuNode> tree) {
        String json = JSONUtil.toJsonStr(tree);
        stringRedisTemplate.opsForValue().set(RedisConstant.MENU_KEY,json,1,TimeUnit.DAYS);
    }

    @Override
    public void setMenuTree(Long userId, List<MenuNode> tree) {
        String json = JSONUtil.toJsonStr(tree);
        stringRedisTemplate.opsForValue().set(RedisConstant.USER_MENU_PREFIX+userId,json,1,TimeUnit.DAYS);
    }

    @Override
    public List<MenuNode> getMenuTree() {
        String json = stringRedisTemplate.opsForValue().get(RedisConstant.MENU_KEY);
        stringRedisTemplate.expire(RedisConstant.MENU_KEY,1,TimeUnit.DAYS);
        return JSONUtil.toList(json, MenuNode.class);
    }

    @Override
    public List<MenuNode> getMenuTree(Long userId) {
        String json = stringRedisTemplate.opsForValue().get(RedisConstant.USER_MENU_PREFIX+userId);
        stringRedisTemplate.expire(RedisConstant.USER_MENU_PREFIX+userId,1,TimeUnit.DAYS);
        return JSONUtil.toList(json, MenuNode.class);
    }

    @Override
    public void removeMenuTree() {
        stringRedisTemplate.delete(RedisConstant.MENU_KEY);
    }

    @Override
    public void removeMenuTree(Long userId) {
        stringRedisTemplate.delete(RedisConstant.USER_MENU_PREFIX+userId);
    }

    @Override
    public void setUser(Long userId, UserVo userVo) {
        String key = RedisConstant.USER_INFO_PREFIX+userId;
        redisTemplate.opsForValue().set(key,userVo,1,TimeUnit.DAYS);
    }

    @Override
    public UserVo getUser(Long userId) {
        String key = RedisConstant.USER_INFO_PREFIX+userId;
        UserVo user = (UserVo) redisTemplate.opsForValue().get(key);
        redisTemplate.expire(key,1,TimeUnit.DAYS);
        return user;
    }

    @Override
    public void removeUser(Long userId) {
        String key = RedisConstant.USER_INFO_PREFIX+userId;
        redisTemplate.delete(key);
    }

    @Override
    public void setPermission(Long userId, List<String> permissions) {
        String key = RedisConstant.USER_PERMISSION_PREFIX+userId;
        redisTemplate.opsForValue().set(key,permissions,1,TimeUnit.DAYS);
    }

    @Override
    public List<String> getPermission(Long userId) {
        String key = RedisConstant.USER_PERMISSION_PREFIX+userId;
        List<String> permissions = (List<String>) redisTemplate.opsForValue().get(key);
        redisTemplate.expire(key,1,TimeUnit.DAYS);
        return permissions;
    }

    @Override
    public void removePermission(Long userId) {
        String key = RedisConstant.USER_PERMISSION_PREFIX+userId;
        redisTemplate.delete(key);
    }

    @Override
    public void setVerify(String key, String code) {
        key = RedisConstant.USER_VERIFY_PREFIX+key;
        stringRedisTemplate.opsForValue().set(key,code,1,TimeUnit.MINUTES);
    }

    @Override
    public String getVerify(String key) {
        key = RedisConstant.USER_VERIFY_PREFIX+key;
        return stringRedisTemplate.opsForValue().get(key);
    }
}
